package com.zhangyu.shadow.core.delegate.web;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;

import com.zhangyu.shadow.core.app.ConfigType;
import com.zhangyu.shadow.core.app.Shadow;
import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.core.delegate.web.route.RouteKey;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * @author zhangyu
 * @date 2017/11/9
 */

public abstract class WebDelegate extends ShadowDelegate implements IWebViewInitialize {
    private WebView mWebView;
    private final ReferenceQueue<WebView> mViewReferenceQueue = new ReferenceQueue<>();
    private String mUrl;
    private boolean mIsWebViewAvailable;
    private ShadowDelegate mTopDelegate;

    public WebDelegate() {
    }

    public abstract IWebViewInitialize setInitialize();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        mUrl = args.getString(RouteKey.URL.name());
        initWebView();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultHorizontalAnimator();
    }

    @SuppressLint("JavascriptInterface")
    private void initWebView() {
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
        }
        final IWebViewInitialize initialize = setInitialize();
        if (initialize != null) {
            final WeakReference<WebView> weakReference = new WeakReference<WebView>(
                    new WebView(getContext()), mViewReferenceQueue);
            mWebView = weakReference.get();
            mWebView = initialize.initWebView(mWebView);
            mWebView.setWebViewClient(initialize.initWebViewClient());
            mWebView.setWebChromeClient(initialize.initWebChromeClient());
            final String name = Shadow.getConfiguration(ConfigType.JAVASCRIPT_INTERFACE);
            mWebView.addJavascriptInterface(ShadowWebInterface.create(this), name);
            mIsWebViewAvailable = true;
        } else {
            throw new NullPointerException("Initialize is null !");
        }
    }

    public void setTopDelegate(ShadowDelegate delegate) {
        mTopDelegate = delegate;
    }

    public ShadowDelegate getTopDelegate() {
        if (mTopDelegate == null) {
            mTopDelegate = this;
        }
        return mTopDelegate;
    }

    public String getUrl() {
        return mUrl;
    }

    /**
     * Gets the WebView.
     */
    public WebView getWebView() {
        return mIsWebViewAvailable ? mWebView : null;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mWebView != null) {
            mWebView.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mWebView != null) {
            mWebView.onResume();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsWebViewAvailable = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
            mWebView = null;
        }
    }
}
